import 'dart:async';

import 'package:flutter/material.dart';
import 'package:heven/model/goods_model.dart';
import 'package:heven/utils/buyEngin.dart';
import 'package:heven/utils/index.dart';

class PopupWidget extends StatefulWidget {
  const PopupWidget({super.key, this.time = 0, this.goods, this.buy});
  final int time;
  final Goods? goods;
  final Function? buy;
  @override
  State<PopupWidget> createState() => _PopupWidgetState();
}

class _PopupWidgetState extends State<PopupWidget> {
  int _total = 60 * 60 * 60 * 1000;
  late Timer _time;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _countdown();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();

    _time.cancel();
  }

  void _countdown() async {
    var time = widget.time; //;
    int millisecond = Common.getMillisecond();

    if (time == 0) {
      time = millisecond;
      Common.saveStorageValue('time', millisecond.toString());
    } else {
      _total = _total - (millisecond - time);
    }

    _time = Timer.periodic(const Duration(seconds: 1), (Timer timer) {
      //   print('每秒触发一次！');

      setState(() {
        _total = _total - 1000;
      });
      if (_total == 0) {
        timer.cancel(); // 取消定时器R
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      alignment: Alignment.center,
      child: Stack(
        children: [
          Container(
            width: double.infinity,
            padding: const EdgeInsets.fromLTRB(40, 41, 40, 41),
            child: Container(
              height: 345,
              alignment: Alignment.center,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(12),
                gradient: const LinearGradient(colors: [
                  Color(0xFFA5D400), // This is the '#145221' color
                  Color(0xFF8CFF88), // This is the '#060606' color
                  Color(0xFF0BD1C7),
                ]),
              ),
              child: Column(
                children: [
                  Container(
                    padding: const EdgeInsets.only(top: 15, bottom: 10),
                    child: Center(
                      child: Image.asset(
                        'assets/images/vip_title.png',
                        width: 163,
                        height: 21,
                      ),
                    ),
                  ),
                  Container(
                    width: 115,
                    height: 22,
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(10),
                      color: const Color(0xFFFFFFFF),
                    ),
                    child: Center(
                      child: Text(
                        "${Common.formatDuration(_total)}",
                        style: const TextStyle(
                          fontSize: 12,
                          color: Color(0xFFFF6F00),
                          decoration: TextDecoration.none,
                        ),
                      ),
                    ),
                  ),
                  Container(
                    margin: const EdgeInsets.fromLTRB(30, 13, 30, 11),
                    child: Image.asset('assets/images/vip_img.png'),
                  ),
                  Container(
                    width: 230,
                    height: 57,
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(35),
                      color: const Color(0xFF000000),
                    ),
                    child: GestureDetector(
                      child: Center(
                        child: Text(
                          '立即抢 ￥${widget.goods!.price}/年',
                          style: const TextStyle(
                            fontSize: 18,
                            fontWeight: FontWeight.w600,
                            color: Color(0xFFFFEC00),
                            decoration: TextDecoration.none,
                          ),
                        ),
                      ),
                      onTap: () async {
                        // Common.loadingDialog(context, '支付中...');
                        // widget.buyEngin?.buyProduct(widget.goods!.customId);
                        widget.buy!(widget.goods!.customId);
                      },
                    ),
                  )
                ],
              ),
            ),
          ),
          Positioned(
            top: 6,
            left: 15,
            child: Image.asset(
              'assets/images/vip_music.png',
              width: 81,
              height: 81,
            ),
          ),
          Positioned(
            top: 0,
            right: 40,
            child: GestureDetector(
              child: Image.asset(
                'assets/images/vip_close.png',
                width: 35,
                height: 35,
              ),
              onTap: () {
                Navigator.pop(context);
                Navigator.pop(context);
              },
            ),
          ),
        ],
      ),
    );
  }
}
